// component/j-details/j-details.js
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		keyCatid: {
			type: Number,
			value: 0,
			observer: function (newVal, oldVal) {
				var that = this;
				wx.request({
					url: app.globalData.host + 'index.php/api/goods/goodsInfo?admin_id=' + app.globalData.admin_id + '&type=' + newVal + '&code=3',
					success: function (e) {
						// console.log(e)
						that.setData({
							items: e.data.result[0]
						})
						// console.log(e.data.result[0])
						var article = e.data.result[0].goods_content;
						WxParse.wxParse('article', 'html', article, that, 8);
					}
				})
			}
		},
		keyGoodsid: {
			type: Number,
			value: 0,
			observer: function (newVal, oldVal) {
				var that = this;
				wx.request({
					url: app.globalData.host + 'index.php/api/goods/goodsInfo?admin_id=' + app.globalData.admin_id + '&goods_id=' + newVal,
					success: function (e) {
						console.log(e)
						that.setData({
							items: e.data.result[0]
						})
						// console.log(e.data.result[0])
						var article = e.data.result[0].goods_content;
						WxParse.wxParse('article', 'html', article, that, 8);
					}
				})
			}
		},
		hasHeader: {
			type: Boolean,
			value: false
		},
		align: {
			type: String,
			value: 'left'
		},
		headerHeight: {
			type: Number,
			value: 400
		},
		hasPay: {
			type: Boolean,
			value: false
		},
	},

	/**
	 * 组件的初始数据
	 */
	data: {

	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		pay: function (e) {
			console.log(e)
			var that = this
			if (that.data.items.shop_price <= 0){
				return false
			}
			wx.chooseAddress({
				success: function (r) {
					var name = r.userName;
					wx.request({
						url: 'https://xcxu.we-fs.com/index.php/api/goods/zhifus?name=' + name + '&sj=' + r.telNumber + '&openid=' + app.globalData.openid + '&price=' + that.data.items.shop_price + '&goods_name=' + that.data.items.goods_name + '&mch_id=' + app.globalData.mch + '&appid=' + app.globalData.appid,
						data: {},
						method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
						// header: {}, // 设置请求的 header
						success: function (res) {
							console.log('rs====' + that.data.items.shop_price);
							console.log('支付====' + that.data.items.goods_name);

							wx.requestPayment(
								{
									'appId': app.globalData.appid,
									'timeStamp': res.data.timeStamp,
									'nonceStr': res.data.nonceStr,
									'package': 'prepay_id=' + res.data.prepayid,
									'signType': 'MD5',
									'paySign': res.data.sign,
									'success': function (res) {
										wx.request({
											url: 'https://xcxu.we-fs.com/index.php/api/goods/order1',
											method: 'post',
											data: {
												'name': name,
												'sj': r.telNumber,
												'goods_name': that.data.items.goods_name,
												'price': that.data.items.shop_price,
												// 'liuyan': e.currentTarget.dataset.liuyan,
												// 'dz': r.provinceName + r.cityName + r.countyName + r.detailInfo,
												'admin_id': app.globalData.admin_id,
												'goods_id': that.data.items.goods_id,
												'openid': app.globalData.openid
											},
											header: {
												'content-type': 'application/x-www-form-urlencoded'
											},
											success: function (res) {
												console.log(res.data)
												that.setData({
													list: res.data[0],
												})
												setTimeout(function () {
													that.setData({
														loadingHidden: true
													})
												}, 1500)
											}
										})
									},
									'fail': function (res) {
										console.log('失败！' + res.errMsg)
									}
								})
						},
						fail: function () {
						},
						complete: function () {
						}
					})

				}
			})
		},
	}
})
